$(function () {

    var role_datagrid = $("#role_datagrid");
    var allPermission = $("#allPermission");
    var selectedPermission = $("#selectedPermission");
    var role_dialog = $("#role_dialog");
    var role_form = $("#role_form");


    role_datagrid.datagrid({
        fitColumns: true,
        fit: true,
        toolbar: '#role_toolbar',
        url: '/role/list.do',
        pagination: true,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        columns: [[
            {title: '角色编号', field: 'sn', width: '500'},
            {title: '角色名称', field: 'name', width: '500'}
        ]]
    })

    allPermission.datagrid({
        fitColumns: true,
        title: '所有权限',
        left: 'center',
        width: 200,
        height: 300,
        scrollbarSize: 13,
        url: '/permission/selectAll.do',
        striped: true,
        rownumbers: true,
        singleSelect: true,
        columns: [[
            {title: '权限名称', field: 'name', width: '250'}
        ]],
        onClickRow: function (index, row) {
            allPermission.datagrid("deleteRow", index);
            selectedPermission.datagrid("appendRow", row);
        }
    })
    selectedPermission.datagrid({
        fitColumns: true,
        width: 200,
        height: 300,
        title: '已有权限',
        url: '/permission/selected.do',
        scrollbarSize: 13,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        columns: [[
            {title: '权限名称', field: 'name', width: '250'}
        ]],
        onClickRow: function (index, row) {
            selectedPermission.datagrid("deleteRow", index);
            allPermission.datagrid("appendRow", row);
        },
        onLoadSuccess: function () {
            var rows = selectedPermission.datagrid("getRows");
            var all = allPermission.datagrid("getRows");
            $(rows).each(function (index, value) {
                for (i = all.length - 1; i >= 0; i--) {
                    if (value.id == all[i].id) {
                        allPermission.datagrid("deleteRow", i);
                    }
                }
            })

        }
    })


    role_dialog.dialog({
        width: 550,
        height: 450,
        buttons: '#role_buttons',
        closed: true,
        onClose: function () {
            selectedPermission.datagrid("loadData", []);
            allPermission.datagrid("load");
        }

    })

    var methodObj = {
        //添加按钮
        add: function () {
            role_dialog.dialog("open");
            role_dialog.dialog('setTitle', '新增角色');
        },
        //编辑按钮
        edit: function () {
            var data = role_datagrid.datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }
            selectedPermission.datagrid("load", {id: data.id})
            role_form.form('load', data);

            role_dialog.dialog("open");
            role_dialog.dialog('setTitle', '编辑角色');
        },
        //删除按钮
        remove: function () {
            var row = role_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要删除的角色信息", "info");
                return;
            }
            $.messager.confirm("温馨提示", "你确定要删除该角色吗？", function (r) {
                if (r) {
                    $.get("/role/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "删除成功", "info", function () {
                                role_datagrid.datagrid("reload");
                            })
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    });
                }
            })
        },
        //刷新按钮
        reload: function () {
            role_datagrid.datagrid("reload");
        },
        //取消按钮
        cancel: function () {
            role_dialog.dialog("close");
        },
        //添加保存按钮
        save: function () {
            role_form.form('submit', {
                url: '/role/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {
                            methodObj.cancel();
                            role_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg);
                    }
                },
                onSubmit: function (param) {
                    var rows = selectedPermission.datagrid("getRows");
                    $(rows).each(function (index, value) {
                        param["permissions[" + index + "].id"] = value.id;
                    })

                }
            })
        }
    }

    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    })

})

